package com.gift.service.Impl;


import com.gift.common.MD5Utility;
import com.gift.common.RequestHolder;
import com.gift.common.ServerResponse;
import com.gift.dao.UserTableMapper;
import com.gift.pojo.UserTable;
import com.gift.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created by Administrator on 2017/12/1.
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    UserTableMapper userTableMapper;

    @Override
    public String login(String loginName, String passWord) {

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime guoqi = LocalDateTime.parse("2018-03-01 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        UsernamePasswordToken token = new UsernamePasswordToken(loginName, passWord);
        Subject currentUser = SecurityUtils.getSubject();
        HttpSession session = RequestHolder.getCurrentRequest().getSession();

        if(now.isBefore(guoqi)){
            try {
                currentUser.login(token);
            } catch (UnknownAccountException uae) {
                session.setAttribute("loginerror", "账号或密码错误!");
            } catch (IncorrectCredentialsException ice) {
                session.setAttribute("loginerror", "账号或密码错误!");
            } catch (DisabledAccountException dae) {
                session.setAttribute("loginerror", "此账号无权登录!");
            } catch (ShiroException se) {
                session.setAttribute("loginerror", "错误!不允许登录!");
            }
        }else{
            session.setAttribute("loginerror", "软件过期,请联系管理员");
        }


        if (session.getAttribute("loginerror") == null || session.getAttribute("loginerror") == "") {

            return "index";
        }
        return "login";
    }


    @Override
    public String newOne(UserTable user) {
        HttpSession session=RequestHolder.getCurrentRequest().getSession();
        //验证登录名
        int result1 = userTableMapper.checkName(user.getLoginname());
        if (result1 > 0) {
            session.setAttribute("registinfomsg","登录名已存在！");
            return  "registinfo";

        }
        //验证邮箱
        int result2 = userTableMapper.checkEmail(user.getEmail());
        if (result2 > 0) {
            session.setAttribute("registinfomsg","注册邮箱已存在！");
            return  "registinfo";
        }
        //加密
        user.setPassword(MD5Utility.encrypt(user.getPassword()));
        int result = userTableMapper.insert(user);
        if (result != 1) {
            session.setAttribute("registinfomsg","注册失败！");
            return  "registinfo";
        }
        session.setAttribute("loginerror","注册成功！");
        return  "login";
    }



}
